package com.fyzk.activity.main;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.fyzk.base.BaseActivity;
import com.fyzk.activity.lottery.basketball.JCLQFragmentActivityI;
import com.fyzk.activity.lottery.gaopin.AnHuiK3Activity;
import com.fyzk.activity.lottery.gaopin.GD11x5Activity;
import com.fyzk.activity.lottery.gaopin.SD11x5Activity;
import com.fyzk.activity.lottery.gaopin.NXK3Activity;
import com.fyzk.activity.lottery.gaopin.SSCActivity;
import com.fyzk.activity.lottery.jc.football.newJz.FootballFragmentActivityI;
import com.fyzk.activity.lottery.shuzicai.DLTActivity;
import com.fyzk.activity.lottery.shuzicai.FC3DActivity;
import com.fyzk.activity.lottery.shuzicai.PL3Activity;
import com.fyzk.activity.lottery.shuzicai.PL5Activity;
import com.fyzk.activity.lottery.shuzicai.QLCActivity;
import com.fyzk.activity.lottery.shuzicai.QXCActivity;
import com.fyzk.activity.lottery.shuzicai.SSQActivity;
import com.fyzk.activity.lottery.zc_new.OldFootballBQCActivity_New;
import com.fyzk.activity.lottery.zc_new.OldFootballJQCActivity_New;
import com.fyzk.activity.lottery.zc_new.OldFootballR9Activity_New;
import com.fyzk.activity.lottery.zc_new.OldFootballSFCActivity_New;
import com.fyzk.data.preference.MySharedPreferences;
import com.tyjc.lottery.R;
import com.fyzk.utils.L;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.TopTitleView;
import com.fyzk.view.pagercycle.ADInfo;

/**
 * 活动页面
 */
public class PromotionDetailActivity extends BaseActivity {

    private TopTitleView topView;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newsdetail_layout);
        topView = (TopTitleView) findViewById(R.id.news_toptitle);
        topView.setRightVisible(View.GONE);
        topView.setOnTitleClickListener(this);
        ADInfo info = (ADInfo) getIntent().getSerializableExtra("news");
        if (TextUtils.isEmpty(info.title)) {
            topView.setCenterText("活动");
        } else {
            topView.setCenterText(info.title);
        }

        webView = (WebView) findViewById(R.id.news_webview);
        if (MySharedPreferences.getInstance().getLoginStatus()) {
            webView.loadUrl(info.click_url);
        } else {
            webView.loadUrl(info.click_url + "?tk=" + MySharedPreferences.getInstance().getAuthtoken());
        }
        WebSettings webSettings = webView.getSettings();
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSaveFormData(false);
        // 必须要打开javaScript
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);

        webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition,
                                        String mimetype, long contentLength) {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });

        webView.setWebViewClient(new MyWebViewClient());
        webView.setWebChromeClient(new MyWebChromeClient());

    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            LogUtil.d("shouldOverrideUrlLoading>>>>" + url);
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
            return super.shouldOverrideKeyEvent(view, event);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            LogUtil.d("onPageFinished>>>>" + url);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    }


    /**
     * Provides a hook for calling "alert" from javascript. Useful for
     * 从javascript中提供了一个叫“提示框” 。这是很有用的
     * debugging your javascript.
     * 调试你的javascript。
     */
    final class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//			AppToast.getToast().show("html click22  " + message);
            int lotteryId = 3010;
            try {
                lotteryId = Integer.parseInt(message);
            } catch (Exception e) {
                L.i(TAG, e.getMessage());
            }

            toBuy(lotteryId);
            result.confirm();
            return true;
        }
    }

    private void toBuy(int lotteryId) {
        Intent intent = new Intent();
        switch (lotteryId) {
            case LotteryIDUtils.SSQ_LOTTERY_ID_INT:
                intent.setClass(getContext(), SSQActivity.class);
                break;
            case LotteryIDUtils.DLT_LOTTERY_ID_INT:
                intent.setClass(getContext(), DLTActivity.class);
                break;
            case LotteryIDUtils.QXC_LOTTERY_ID_INT:
                intent.setClass(getContext(), QXCActivity.class);
                break;
            case LotteryIDUtils.JZSPF_LOTTERY_ID_INT:
            case LotteryIDUtils.JZRFSPF_LOTTERY_ID_INT:
            case LotteryIDUtils.JZBF_LOTTERY_ID_INT:
            case LotteryIDUtils.JZBQC_LOTTERY_ID_INT:
            case LotteryIDUtils.JZZJQ_LOTTERY_ID_INT:
            case LotteryIDUtils.JZHH_LOTTERY_ID_INT:
                intent.setClass(getContext(), FootballFragmentActivityI.class);
                break;
            case LotteryIDUtils.JZDG_LOTTERY_ID_INT:
                // 竞猜足球单关
                intent.setClass(getContext(), FootballFragmentActivityI.class);
                intent.putExtra("isDan", true);
                break;
            case LotteryIDUtils.ZCSFC_LOTTERY_ID_INT:
                // intent.setClass(getContext(), SFCActivity.class);
                intent.setClass(getContext(), OldFootballSFCActivity_New.class);
                break;
            case LotteryIDUtils.ZCRXJ_LOTTERY_ID_INT:
                // intent.setClass(getContext(), R9Activity.class);
                intent.setClass(getContext(), OldFootballR9Activity_New.class);
                break;
            case LotteryIDUtils.PL3_LOTTERY_ID_INT:
                intent.setClass(getContext(), PL3Activity.class);
                break;
            case LotteryIDUtils.PL5_LOTTERY_ID_INT:
                intent.setClass(getContext(), PL5Activity.class);
                break;
            case LotteryIDUtils.FC3D_LOTTERY_ID_INT:
                intent.setClass(getContext(), FC3DActivity.class);
                break;
            case LotteryIDUtils.ZCBQC_LOTTERY_ID_INT:
                // intent.setClass(getContext(), LCBQCActivity.class);
                intent.setClass(getContext(), OldFootballBQCActivity_New.class);
                break;
            case LotteryIDUtils.QLC_LOTTERY_ID_INT:
                intent.setClass(getContext(), QLCActivity.class);
                break;
            case LotteryIDUtils.ZCJQC_LOTTERY_ID_INT:
                // intent.setClass(getContext(), JQCActivity.class);
                intent.setClass(getContext(), OldFootballJQCActivity_New.class);
                break;
            case LotteryIDUtils.SD11XUAN5_LOTTERY_ID_INT:
                intent.setClass(getContext(), SD11x5Activity.class);
                break;
            case LotteryIDUtils.GD11XUAN5_LOTTERY_ID_INT:
                intent.setClass(getContext(), GD11x5Activity.class);
                break;
            case LotteryIDUtils.CQSSC_LOTTERY_ID_INT:
                intent.setClass(getContext(), SSCActivity.class);
                break;
            case LotteryIDUtils.NXK3_LOTTERY_ID_INT:
                intent.setClass(getContext(), NXK3Activity.class);
                break;
            case LotteryIDUtils.ANHUIK3_LOTTERY_ID_INT:
                intent.setClass(getContext(), AnHuiK3Activity.class);
                break;
            case LotteryIDUtils.JLSF_LOTTERY_ID_INT:
                intent.setClass(getContext(), JCLQFragmentActivityI.class);
                break;
        }
        try {
            startActivity(intent);
            this.finish();
        } catch (ActivityNotFoundException e) {
            L.i(TAG, e.getMessage());
        }
    }
}
